# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=bareos
pkgver=18.2.8
pkgrel=0
pkgdesc="Bareos - Backup Archiving REcovery Open Sourced"
url="http://www.bareos.org"
# FTBFS on mips
arch="all !mips !mips64"
options="!check"
license="AGPL-3.0"
depends=""
makedepends="cmake gawk
lzo-dev acl-dev gettext-dev
readline-dev ncurses-dev jansson-dev
sqlite-dev postgresql-dev mariadb-connector-c-dev
php7"
install="$pkgname.pre-install $pkgname.post-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-dbg
$pkgname-mysql:_mysql $pkgname-sqlite:_sqlite $pkgname-postgresql:_postgresql
$pkgname-webui:_webui:noarch $pkgname-webui-apache2:_webui_apache2:noarch $pkgname-webui-nginx:_webui_nginx:noarch"
pkgusers=$pkgname
pkggroups=$pkgname
source="$pkgname-$pkgver.tar.gz::https://github.com/$pkgname/$pkgname/archive/Release/$pkgver.tar.gz
	$pkgname-dir.initd
	$pkgname-sd.initd
	$pkgname-fd.initd
	detect-alpine.patch
	add-libintl.patch
	nginx-conf.patch
	path-mounted.patch
	649ae58a65f6cdbcf7ee770a0d5b5cf51b5e412b.patch
	"
builddir="$srcdir"/${pkgname}-Release-${pkgver}

prepare() {
	default_prepare
	cd "$builddir"
	mkdir -pv core/platforms/alpine
	touch core/platforms/alpine/CMakeLists.txt

	# cleanup stale builddir and recreate
	[ -d cmake-build ] && rm -rfv cmake-build
	mkdir cmake-build
}

build() {
	cd "$builddir"/cmake-build

	cmake .. \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		-Dsysconfdir=/etc \
		-Dbasename=$pkgname \
		-Dhostname=localhost \
		-Dconfdir=/etc/$pkgname \
		-Dlibdir=/usr/lib \
		-Darchivedir=/var/lib/$pkgname/archive \
		-Dworkingdir=/var/lib/$pkgname \
		-Dscriptdir=/etc/$pkgname/scripts \
		-Dplugindir=/usr/lib/$pkgname \
		-Dbackenddir=/usr/lib/$pkgname \
		-Dlogdir=/var/log/$pkgname \
		-Dpiddir=/run/$pkgname \
		-Dbsrdir=/run/$pkgname \
		-Dsubsysdir=/var/lock \
		-Dsmartalloc=yes \
		-Dbatch-insert=yes \
		-Dscsi-crypto=yes \
		-Dlmdb=yes \
		-Dipv6=yes \
		-Ddynamic-cats-backends=yes \
		-Dpostgresql=yes \
		-Dmysql=yes \
		-Dsqlite3=yes \
		-Dopenssl=yes \
		-Dtraymonitor=no \
		-Dsystemd=no \
		-Dndmp=no \
		-Ddir-user=$pkgname \
		-Ddir-group=$pkgname \
		-Dsd-user=$pkgname \
		-Dsd-group=$pkgname \
		-Dfd-user=root \
		-Dfd-group=$pkgname

	make
}

package() {
	cd "$builddir"/cmake-build
	make DESTDIR="$pkgdir" install
	mkdir -p "$pkgdir"/usr/sbin
	mkdir -p "$pkgdir"/usr/bin
	mkdir -p "$pkgdir"/var/lib/bareos
	mkdir -p "$pkgdir"/var/log/bareos

	local daemon; for daemon in dir sd fd; do
		install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
			"$pkgdir"/etc/init.d/$pkgname-$daemon
	done

	# Install logrotate script
	install -Dm644 ../core/scripts/logrotate "$pkgdir"/etc/logrotate.d/$pkgname

	# Fix job to use shell script instead of perl script
	sed -i -e 's/make_catalog_backup.pl/make_catalog_backup/' \
		"$pkgdir"/etc/bareos/bareos-dir.d/job/BackupCatalog.conf

	# remove perl script
	rm "$pkgdir"/etc/bareos/scripts/make_catalog_backup.pl

	# remove http configuration
	rm -r "$pkgdir"/etc/httpd
}

_mysql() {
	description="$description (mysql driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-mysql.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates grants updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/mysql*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_sqlite() {
	description="$description (sqlite driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-sqlite3.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/sqlite3*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_postgresql() {
	description="$description (postgresql driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos
	mv "$pkgdir"/usr/lib/bareos/libbareoscats-postgresql.so "$subpkgdir"/usr/lib/bareos/

	for _i in creates grants updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/postgresql*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
}

_webui() {
	description="$description (webui)"
	depends="php7 php7-gettext php7-mbstring php7-session php7-ctype php7-openssl php7-json"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/bareos-webui "$subpkgdir"/usr/share/

	mkdir -p "$subpkgdir"/etc
	mv "$pkgdir"/etc/bareos-webui "$subpkgdir"/etc/

	mkdir -p "$subpkgdir"/etc/bareos/bareos-dir.d/console/
	mv "$pkgdir"/etc/bareos/bareos-dir.d/console/admin.conf.example "$subpkgdir"/etc/bareos/bareos-dir.d/console/

	mkdir -p "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
	mv "$pkgdir"/etc/bareos/bareos-dir.d/profile/webui-admin.conf "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
}

_webui_apache2() {
	description="$description (webui apache2 configuration)"
	depends="$pkgname-webui php7-apache2"

	mkdir -p "$subpkgdir"/etc/apache2/conf.d
	cp "$builddir"/webui/install/apache/bareos-webui.conf "$subpkgdir"/etc/apache2/conf.d/
}


_webui_nginx() {
	description="$description (webui apache2 configuration)"
	depends="$pkgname-webui nginx"

	mkdir -p "$subpkgdir"/etc/nginx/conf.d
	cp "$builddir"/webui/install/nginx/bareos-webui.conf "$subpkgdir"/etc/nginx/conf.d/
}


sha512sums="61c9155a02ec0f3a21e06db4f5701dabb25411219d98940b50912c16ac38ec007584a7ef9728f2f12acf2823339a25eba3c9b34a6fa9e625a1d2d0778653b860  bareos-18.2.8.tar.gz
eb1e7072b579bf9ae21f2e351d6900abb277db64e373f4760bac8188b82929376e4a196d2c935cefe1ae4cc2c396f2fcba1a25642b26e2f92a0d008fbdc4b5f2  bareos-dir.initd
c770b1d041fafef93d4eb0269ba8d9733e85ef465657fe8dd5d5c68a27ec773cec9c5c582d4a16596d95bbf6dbd3f7194dc9c0d8ed73138e9fb438fba9aa9445  bareos-sd.initd
c6347079dbcef5f4a69ec0c4ecc31803520d715d599d89c6bbfbb3741a86c50d7295c30432889b13ee9c16f2feaa84b1c6ae992cfee6505d569c6493d7e85a5b  bareos-fd.initd
1251d81d142b58bb2f5ab2ea762068fe2273aa5face9dfd314011706e9c2189ecab36b80f76317a595679f164b124cff85623c1ec98cc6997e350b6223f2a524  detect-alpine.patch
1439b8f72e39ca27689dbcc1a58c63f22b7dc4d4bee480408a4c388e30c0c3434915e5162fa7e55d16b3a80c9e4727b96263ab5edc9dda52acb38737e136f147  add-libintl.patch
0f73f38237a2320785b565d92c175de2c0c333081723c98b026f188b67ad738b5157222bc233811c6dafda3ddcdabf6849d5eeccc6723e0506e35923309cbcee  nginx-conf.patch
2fb3d059185efb9c492d865ed79810971992ea2d8609639b4260b3338cbd0e89dc8ad0b3031be868d13b21ed9a93ff444703332f0864bbec7ce3e691210d05f7  path-mounted.patch
5bbad4f2b831ac51b41319965675ff7d828ab518eb87b682e91e49fdd10d2ee6580968a5cf79df5bda5f3bad957ce251d40b561a48dab5b32bc217a972d1276b  649ae58a65f6cdbcf7ee770a0d5b5cf51b5e412b.patch"
